package com.mall.user.services;

import com.mall.user.ISendMailService;
import lombok.SneakyThrows;
import lombok.extern.log4j.Log4j;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import javax.mail.internet.MimeMessage;
import java.io.File;

@Service
@Slf4j
public class SendMailServiceImpl implements ISendMailService {

    @Autowired
//    private JavaMailSender javaMailSender;

    // 发件人邮箱
//    @Value("${spring.mail.username}")
//    private String myMail;

    // 收件人邮箱
//    private String receiveMail = "1821299467@qq.com";

    // 消息主题
//    private String messageSubject = "csmall商城激活";

    // 消息内容
//    private String messageText = "http://localhost:8080/user/verify?uid=akjsdnh39udklnw";

//    @SneakyThrows
//    @Override
//    public void sendMail() {
//        // 简单文本的邮件发送
////        SimpleMailMessage mailMessage = new SimpleMailMessage();
////        mailMessage.setFrom(myMail);
////        mailMessage.setTo(receiveMail);
////        mailMessage.setSubject(messageSubject);
////        mailMessage.setText(messageText);
////        javaMailSender.send(mailMessage);
//
//        // 发送其它类型的邮件
////        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
////        MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage, true);
////        // 使用MimeMessageHelper设置邮件信息
////        messageHelper.setFrom(myMail);
////        messageHelper.setTo(receiveMail);
////        messageHelper.setSubject(messageSubject);
////        // html文件
////        messageHelper.setText(htmlText,true);
////        // 指定附件的文件
////        FileSystemResource fileSystemResource = new FileSystemResource(new File("xxx"));
////        // 发送的附件
////        messageHelper.addAttachment("xxx",fileSystemResource);
////        javaMailSender.send(mimeMessage);
//    }

    @Override
    public void sendMail(){
        // 创建子类对象
        MySendMailRunnable sendMailRunnable = new MySendMailRunnable();
        //创建Thread对象，并且把实现了Runnable接口的子类对象作为参数传递
        Thread thread = new Thread(sendMailRunnable);
        //start方法去启动线程
        thread.start();
    }
    /**
     *  使用多线程发送邮件
     *
     */
    // 定义一个类实现Runnable接口
    class MySendMailRunnable implements Runnable{

        @Override
        public void run() {
            // 简单文本的邮件发送
//            SimpleMailMessage mailMessage = new SimpleMailMessage();
//            mailMessage.setFrom(myMail);
//            mailMessage.setTo(receiveMail);
//            mailMessage.setSubject(messageSubject);
//            StringBuilder stringBuilder = new StringBuilder();
//            stringBuilder.append("http://localhost:8080/user/verify?uid=")
//                    .append(uuid).append("&username=").append(username);
//            mailMessage.setText(messageText);
//            javaMailSender.send(mailMessage);
        }
    }

}
